package com.fjd.mapper;

import com.fjd.entity.CustomerAddr;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CustomerAddrMapper {

    /**
     * 新增地址
     * @param customerAddr
     */
    @Insert("insert into customer_addr (address_id, customer_id, street_address, district, city, state, zipcode, country, is_pickup, consignee, phone, is_default) " +
            "values (#{addressId}, #{customerId}, #{streetAddress}, #{district}, #{city}, #{state}, #{zipcode}, '中国', #{isPickup}, #{consignee}, #{phone}, #{isDefault})")
    void insert(CustomerAddr customerAddr);

    /**
     * 根据用户Id查询地址
     * @param customerId
     * @return
     */
    List<CustomerAddr> getByCustomerId(String customerId);

    /**
     * 根据地址Id查询地址
     * @param addressId
     * @return
     */
    @Select("select * from customer_addr where address_id = #{addressId}")
    CustomerAddr getByAddressId(String addressId);

    /**
     * 修改地址
     * @param customerAddr
     */
    void  update(CustomerAddr customerAddr);

    /**
     * 根据用户Id设置所有地址默认为0
     * @param customerAddr
     */
    @Update("update customer_addr set is_default = 0 where customer_id = #{customerId}")
    void updateIsDefaultByCustomerId(CustomerAddr customerAddr);

    /**
     * 根据地址Id设置地址为默认
     * @param customerAddr
     */
    @Update("update customer_addr set is_default = 1 where address_id = #{addressId}")
    void setDefaultByAddressId(CustomerAddr customerAddr);

    /**
     * 根据地址Id删除地址
     * @param addressId
     */
    @Delete("delete from customer_addr where address_id = #{addressId}")
    void deleteByAddressId(String addressId);

    /**
     * 根据地址Id查询是否为默认
     * @param addressId
     * @return
     */
    @Select("select is_default from customer_addr where address_id = #{addressId}")
    boolean getDefaultByAddrId(String addressId);

    @Select("select * from customer_addr where customer_id =#{customerId} and is_default = 1")
    CustomerAddr getDefaultByCustomerId(String customerId);

    @Select("select * from customer_addr where is_pickup = 1 ")
    List<CustomerAddr> listpickup();
}
